package com.fyzk.activity.lottery.shuzicai;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.fyzk.base.BaseActivity;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

public class WebViewActivity extends BaseActivity implements
        onTopTitleClickListener {

    private String title = "";
    private String webId = "";

    private TopTitleView topView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_info_detail_webview);
        getInfo();

    }

    private void getInfo() {
        Intent intent = getIntent();
        if (intent != null) {
            title = intent.getStringExtra(IntentParamUtils.TITLE);
            webId = intent.getStringExtra(IntentParamUtils.URL);
        }
        initView(title, webId);
    }

    private void initView(String title, String webId) {
        topView = (TopTitleView) findViewById(R.id.topTitle);
        topView.setRightVisible(View.GONE);
        topView.setCenterText(title);
        topView.setOnTitleClickListener(this);
        initWebView(webId);
    }

    public void initWebView(String webId) {
        WebView webView = (WebView) findViewById(R.id.ruyipackage_webview);
        String url = webId;
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
        webView.setWebViewClient(new NoAdWebViewClient());
    }

    public void onPause() {
        super.onPause();
    }

    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    public void onLeftClick() {
        // TODO Auto-generated method stub
        finish();
    }

    @Override
    public void onCenterClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRightClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRight2Click() {
        // TODO Auto-generated method stub

    }

    public class NoAdWebViewClient extends WebViewClient {
        public NoAdWebViewClient() {
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String js = getClearAdDivJs();
            view.loadUrl(js); //加载js方法代码
            view.loadUrl("javascript:hideAd();"); //调用js方法
        }

        public String getClearAdDivJs() {
            String js = "javascript:function hideAd() {";
            js += "var adDiv = document.getElementsByClassName('header changeSkinColor')[0].style.display = 'none';";
            js += "}";
            return js;
        }
    }


}
